//
//  EditProductViewController.m
//  BusinessApp
//
//  Created by macox on 16/9/28.
//  Copyright © 2016年 macox. All rights reserved.
//

#import "EditProductViewController.h"
#import "SDCycleScrollView.h"
#import "ProductModelInfo.h"
@interface EditProductViewController ()<UITextFieldDelegate,UITextViewDelegate,SDCycleScrollViewDelegate>


@property (weak, nonatomic) IBOutlet UIScrollView *EditProductScrollV;
@property (nonatomic, strong) NSMutableArray *goodDataListArr;
@property (nonatomic , strong) NSMutableArray *imageArr;
@property (nonatomic,strong)ProductModelInfo *Datamodel;
@end

@implementation EditProductViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        _imageArr = [NSMutableArray array];
      [self requestDateProductDetail];
    self.navigationItem.title = @"商品详情";
    UIImage *classBackImage = [UIImage imageNamed:@"zuojiantou@3x-1"];
    classBackImage = [classBackImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:classBackImage style:UIBarButtonItemStylePlain target:self action:@selector(classBackAction)];
//    _ProductPriceFiled.delegate = self;
//    _StockFiled.delegate = self;
//   _ProductPriceFiled.delegate = self;
//    _Reason.textColor = [UIColor grayColor];
//    _Reason.text = @"审核未通过原因/违规下架原因";
//    _Reason.delegate = self;
    
//    [self ShopDetailsAction];
  
    // Do any additional setup after loading the view from its nib.
}
- (void)requestDateProductDetail{

    _goodDataListArr = [NSMutableArray array];
    NSDictionary *param = @{@"ShouJiHao":GETPHONE,@"Duuid":GETUUID,@"Token":GETTOKEN,@"CpId":self.ProductID};
    [GDJHttpTool get:ProductInfosss params:param success:^(id responseObj) {
        NSLog(@"+++++++++________%@", responseObj);
        NSLog(@"=====---------------%@", param);
        if ([[responseObj objectForKey:@"Err"] intValue]) {
            
        }else{
            
            
            NSDictionary *dataArr = [responseObj objectForKey:@"Product"];
            
            self.Datamodel = [ProductModelInfo mj_objectWithKeyValues:dataArr];
            _ProductName.text = self.Datamodel.ProductName;
            _ProductClassitfy.text = self.Datamodel.CategoryNames;
            _SaleProductClassitfy.text = self.Datamodel.CategoryNames;
            _ProductPrice.text = [NSString stringWithFormat:@"%.2f",self.Datamodel.MarketPrice.doubleValue ];
            _MinPrice.text = [NSString stringWithFormat:@"%.2f",self.Datamodel.SalePrice.doubleValue ];
            _MeasureUnit.text = self.Datamodel.MeasureUnit;
            _Stock.text =[NSString stringWithFormat:@"%@",self.Datamodel.Stock] ;
            _ProductDescriptionInfo.text = self.Datamodel.ProductDescriptionInfo;
            if ([[NSString stringWithFormat:@"%@", self.Datamodel.AuditStatus] isEqualToString:@"1" ]) {
                            _Status.text =  @"待审核";
            } else if ([[NSString stringWithFormat:@"%@", self.Datamodel.AuditStatus] isEqualToString:@"2" ]) {
                _Status.text =  @"销售中";
            }else if ([[NSString stringWithFormat:@"%@", self.Datamodel.AuditStatus] isEqualToString:@"3" ]) {
                _Status.text =  @"未通过";
            }else if ([[NSString stringWithFormat:@"%@", self.Datamodel.AuditStatus] isEqualToString:@"4" ]) {
                _Status.textColor = [UIColor redColor];
                _Status.text =  @"违规下架";
            }else if ([[NSString stringWithFormat:@"%@", self.Datamodel.AuditStatus] isEqualToString:@"5" ]) {
                _Status.text =  @"未审核";
            }
            _Reason.text = self.Datamodel.AuditReason;
            
            
            
            SDCycleScrollView *cycleScrollView =    [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScrernWidth, 220) delegate:self placeholderImage:[UIImage imageNamed:@"图片加载"]];
            cycleScrollView.delegate = self;
            cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
            //刷新轮播图
            NSMutableArray *arr = [NSMutableArray array];
            for (NSString *str in self.Datamodel.imagePath) {
                [arr addObject:[NSString stringWithFormat:@"%@%@",ReleaseORDebug,str]];
            }

            cycleScrollView.showPageControl = YES;
            cycleScrollView.currentPageDotColor = [UIColor whiteColor];
            cycleScrollView.imageURLStringsGroup = arr;
            cycleScrollView.showPageControl = YES;
            cycleScrollView.backgroundColor = [UIColor whiteColor];
            
            [_EditProductScrollV addSubview:cycleScrollView];

            
        }

    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}
#pragma mark -- 返回
- (void)classBackAction{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - VC将要出现
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.tabBarController.tabBar.hidden = YES;
    //获取通知中心单例对象
//    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
    /*
     Reduce the size of the text view so that it's not obscured by the keyboard.
     Animate the resize so that it's in sync with the appearance of the keyboard.
     */
    NSDictionary *userInfo = [notification userInfo];
    // Get the origin of the keyboard when it's displayed.
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
    
    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect tableViewFrame = self.view.bounds;
    tableViewFrame.size.height = keyboardTop - self.view.bounds.origin.y;
    
    // Get the duration of the animation.
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    
    // Animate the resize of the text view's frame in sync with the keyboard's appearance.
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    
   _EditProductScrollV.frame = CGRectMake(0,0,kScrernWidth, keyboardTop+20);
    [UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notification {
    
    NSDictionary* userInfo = [notification userInfo];
    /*
     Restore the size of the text view (fill self's view).
     Animate the resize so that it's in sync with the disappearance of the keyboard.
     */
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    _EditProductScrollV.frame= CGRectMake(0,0,kScrernWidth, kScrernHeight);
    [UIView commitAnimations];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    return YES;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:@"审核未通过原因/违规下架原因"]) {
        textView.text = @"";
    }
}
- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length<1) {
        textView.text = @"审核未通过原因/违规下架原因";
    }
}
#pragma mark - 轮播图
//- (void)ShopDetailsAction{
//    
//    // 情景一：采用本地图片实现
//
// SDCycleScrollView *cycleScrollView =    [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScrernWidth, 180) delegate:self placeholderImage:[UIImage imageNamed:@"占位图3"]];
////    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScrernWidth, 180) shouldInfiniteLoop:YES imageNamesGroup:imageNames];
//    cycleScrollView.delegate = self;
//    cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolStyleAnimated;
//    //刷新轮播图
//    NSMutableArray *arr = [NSMutableArray array];
//    for (NSString *str in _imageArr) {
//        [arr addObject:[NSString stringWithFormat:@"%@%@",ReleaseORDebug,str]];
//    }
//    NSLog(@"woowowowoow%lu",(unsigned long)_imageArr.count);
//    cycleScrollView.showPageControl = YES;
//    cycleScrollView.currentPageDotColor = [UIColor whiteColor];
//    cycleScrollView.imageURLStringsGroup = arr;
//    cycleScrollView.showPageControl = YES;
//    cycleScrollView.backgroundColor = [UIColor whiteColor];
//
//        [_EditProductScrollV addSubview:cycleScrollView];
//    
//    
//
//
//
//}


#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    NSLog(@"---点击了第%ld张图片", (long)index);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
